package com.mxpio.erp.technology.controller;

import com.mxpio.erp.common.technology.entity.BomInfoLine;
import com.mxpio.erp.common.technology.service.BomInfoLineService;
import com.mxpio.erp.common.technology.service.BomInfoService;
import com.mxpio.erp.common.technology.vo.BomInfoLineVO;
import com.mxpioframework.common.exception.MBootException;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Tag(name = "BomInfoLineController", description = "Bom行接口")
@RestController
@RequestMapping("/erp/tech/bomline/")
public class BomInfoLineController {
    @Autowired
    private BomInfoLineService bomInfoLineService;
    @Autowired
    private BomInfoService bomInfoService;
    @GetMapping("list")
    @Operation(summary = "Bom行列表", description = "获取Bom行列表（无分页）", method = "GET")
    public Result<List<BomInfoLine>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<BomInfoLine> bomInfoLineList = bomInfoLineService.list(BomInfoLine.class, criteria);
        return Result.OK(bomInfoLineList);
    }

    @GetMapping("page")
    @Operation(summary = "Bom行列表", description = "获取Bom行列表（分页）", method = "GET")
    public Result<Page<BomInfoLine>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<BomInfoLine> bomInfoLineList = bomInfoLineService.listPage(BomInfoLine.class, page, criteria);
        return Result.OK(bomInfoLineList);
    }
    @GetMapping("list/{bomId}")
    @Operation(summary = "根据bomId获取Bom行", description = "根据bomId获取Bom行", method = "GET")
    public Result<List<BomInfoLine>> getByBomId(@Parameter(description="bomId") @PathVariable(name = "bomId", required = true) String bomId) {
        Criteria c = Criteria.create().addCriterion("bomId", Operator.EQ, bomId);
        List<BomInfoLine> bomInfoLineList = bomInfoLineService.list(BomInfoLine.class, c);
        return Result.OK(bomInfoLineList);
    }

    @GetMapping("list/{bomLineId}")
    @Operation(summary = "根据bomLineId获取Bom行", description = "根据bomLineId获取Bom行", method = "GET")
    public Result<BomInfoLine> getById(@Parameter(description="bomLineId") @PathVariable(name = "bomLineId", required = true)String bomLineId) {
        BomInfoLine bomInfoLine = bomInfoLineService.getById(BomInfoLine.class, bomLineId);
        return Result.OK(bomInfoLine);
    }

    @PostMapping("add")
    @Operation(summary = "新增Bom行", description = "新增Bom行", method = "POST")
    public Result<BomInfoLine> add(@RequestBody BomInfoLine bomInfoLine) {
        bomInfoLineService.save(bomInfoLine);
        return Result.OK(bomInfoLine);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑Bom行", description = "编辑Bom行", method = "PUT")
    public Result<BomInfoLine> edit(@RequestBody BomInfoLine bomInfoLine) {
        bomInfoLineService.update(bomInfoLine);
        return Result.OK(bomInfoLine);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除Bom行", description = "删除Bom行,多个id用英文逗号分隔", method = "DELETE")
    public Result<BomInfoLine> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            bomInfoLineService.remove(BomInfoLine.class, key);
        }
        return Result.OK();
    }

    @GetMapping("inversequery")
    @Operation(summary = "反查物料引用", description = "反查物料引用", method = "GET")
    public Result<List<BomInfoLineVO>> inverseQuery (@Parameter(description="itemCode")  String itemCode
            ,@Parameter(description="date",required = true) String date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dated = null;
        try {
            dated = dateFormat.parse(date);
        } catch (Exception e) {
            throw new MBootException("日期" + date + "不合法");
        }
        return bomInfoService.inverseQuery(itemCode,dated);
    }


}
